package com.eco.fanliapp.job.hdkitemjob;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.eco.fanliapp.common.constant.ApiEnum;
import com.eco.fanliapp.dao.*;
import com.eco.fanliapp.entity.*;
import com.eco.fanliapp.service.IAppGoodsService;
import com.eco.fanliapp.service.RedisTemplateService;
import com.eco.fanliapp.utils.ApiUtils;
import com.eco.fanliapp.utils.Convert;
import com.eco.fanliapp.utils.JsonUtils;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@DisallowConcurrentExecution
@PersistJobDataAfterExecution
public class HDKGoodsCategoryTask implements Job, Serializable {
    private static final Logger log = LoggerFactory.getLogger(HDKGoodsCategoryTask.class);


    @Autowired
    private RedisTemplateService redisTemplateService;

    @Autowired
    private AppGoodsClassifyMapper appGoodsClassifyMapper;

    public HDKGoodsCategoryTask() {
    }

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {

        //取得job详情
        JobDetail jobDetail = context.getJobDetail();

        JobDataMap dataMap = jobDetail.getJobDataMap();

        int executeCount = 0;
        if (dataMap.containsKey("runCount")) {
            executeCount = dataMap.getInt("runCount");
        }

        // 增量计数并将其存储回 JobDataMap，这样可以适当保持工作状态
        executeCount++;
        dataMap.put("runCount", executeCount);

        //当前时间
        long startMillions = System.currentTimeMillis();
        log.info("***************************************开始执行商品类目修改定时任务：executeCount=" + executeCount + "--beginTime=" + startMillions);
        int intArray[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 16, 17};
        String nameArray[] = {"女装", "男装", "内衣", "美妆", "配饰", "鞋品", "箱包", "儿童", "母婴", "居家", "美食", "数码", "家电", "车品", "文体", "宠物"};

        for (int y = 0; y < intArray.length; y++) {

            AppGoodsClassify appGoodsClassify = new AppGoodsClassify();
            appGoodsClassify.setCid(intArray[y]);
            List<AppGoodsClassify> lst = appGoodsClassifyMapper.selectAppGoodsClassifyList(appGoodsClassify);
            for (int i = 0; i < nameArray.length; i++) {
                if (i == y) {
                    appGoodsClassify.setCname(nameArray[i]);
                    break;
                }
            }
            appGoodsClassify.setPlatform(1);
            if (lst != null && lst.size() > 0) {
                appGoodsClassify.setTableId(lst.get(0).getTableId());
                int n = appGoodsClassifyMapper.updateAppGoodsClassify(appGoodsClassify);
                if (n > 0) {
                    log.info("更新成功{}" + intArray[y]);
                }

            } else {
                appGoodsClassify.setSort(intArray[y]);
                int n = appGoodsClassifyMapper.insert(appGoodsClassify);
                if (n > 0) {
                    log.info("添加成功{}" + intArray[y]);
                }

            }
        }
        String redisKeyStr = "app_goods_category_list";
        redisTemplateService.del(redisKeyStr);

        long endTime = System.currentTimeMillis();
        log.info("***************************************结束商品类目修改定时任务：executeCount=" + executeCount + "--endTime=" + endTime + "--耗时=" + (endTime - startMillions));

    }

}
